﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Services;
using DailyDiet.Services.Impl;
using DailyDiet.Services.Navigation;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Views;

namespace DailyDiet.ViewModels
{
    public class FavoritePageViewModel:ViewModelBase
    {
        /// <summary>
        /// 依赖注入DietStorageService
        /// </summary>
        private IDietStorageService _dietStorageService;

        private IPreferenceStorage _preferenceStorage;

        /// <summary>
        /// 供演示使用的收藏存储
        /// </summary>
        private IFavoriteStorageService _favoriteStorageService;

        private IContentNavigationService _navigationService;

        public FavoritePageViewModel(IDietStorageService dietStorageService, IPreferenceStorage preferenceStorage, IContentNavigationService navigationService, IFavoriteStorageService favoriteStorageService)
        {
            _dietStorageService = dietStorageService;
            _preferenceStorage = preferenceStorage;
            _navigationService = navigationService;
            _favoriteStorageService = favoriteStorageService;
        }

        public List<Diet> FavoriteDietsList
        {

            get => _favoriteDietsList;
            set => Set(nameof(FavoriteDietsList), ref _favoriteDietsList, value);
        }

        private List<Diet> _favoriteDietsList;

        public List<Favorite> FavoritesList
        {

            get => _favoritesList;
            set => Set(nameof(FavoritesList), ref _favoritesList, value);
        }

        private List<Favorite> _favoritesList;


        /// <summary>
        /// 展示全部菜品的命令
        /// </summary>
        private RelayCommand _showFavoriteDietsCommand;
        public RelayCommand ShowFavoriteDietsCommand =>
            _showFavoriteDietsCommand ?? (_showFavoriteDietsCommand = new RelayCommand(async () => await ShowFavoriteDietsCommandFunc()));

        public async Task ShowFavoriteDietsCommandFunc()
        {

            var favoriteList = await _favoriteStorageService.GetFavoritesAsync();
            var dietTaskList = favoriteList
                .Select(async p => await _dietStorageService.GetDietAsync(p.DietId))
                .ToList();
            var dietsarray = await Task.WhenAll(dietTaskList);
            FavoriteDietsList = dietsarray.ToList();
        }

        /// <summary>
        /// 处理点击事件
        /// </summary>
        private RelayCommand<Diet> _dietTappedCommand;
        public RelayCommand<Diet> DietTappedCommand =>
            _dietTappedCommand ?? (_dietTappedCommand =
                new RelayCommand<Diet>(async diet => await DietTappedCommandFunc(diet)));

        private async Task DietTappedCommandFunc(Diet diet)
        {
            await _navigationService.NavigateToAsync(ContentNavigationServiceConstants.DietsDetailPage, diet);


        }


    
}
}
